Published 2006-03-15 10:14:53
static bool skipUntilBoundary(const string &delimiter,Quite nicely written, and using and doing some quite obtuse test to see if the string stored in the current buffer matched the one being looked for. (I didnt have a look at compareStringToQueue, but my guess is that it just went through the string in the buffer, starting at the delimiter position, and check to see if it matched what was being looked for.
unsigned int *nlines, bool *eof)
{
int endpos = delimiter.length();
char *delimiterqueue = 0;
int delimiterpos = 0;
const char *delimiterStr = delimiter.c_str();
if (delimiter != "") {
delimiterqueue = new char[endpos];
memset(delimiterqueue, 0, endpos);
}
// first, skip to the first delimiter string. Anything between the
// header and the first delimiter string is simply ignored (it's
// usually a text message intended for non-mime clients)
char c;
bool foundBoundary = false;
for (;;) {
if (!mimeSource->getChar(&c)) {
*eof = true;
break;
}
if (c == '\n')
++*nlines;
// if there is no delimiter, we just read until the end of the
// file.
if (!delimiterqueue)
continue;
delimiterqueue[delimiterpos++ % endpos] = c;
if (compareStringToQueue(delimiterStr, delimiterqueue,
delimiterpos, endpos)) {
foundBoundary = true;
break;
}
}
delete [] delimiterqueue;
delimiterqueue = 0;
return foundBoundary;
}
bool skipUntilBoundary(MimeStream mimeSource, char[] delimiter,our function signature is slighly different here:
inout uint nlines, inout bool eof)
{
char[] teststring = "";Next up is creating a test string, to store our buffer to test against, the second line of this ensures that it's size is fixed at twice that of the original delimiter. (which should be more than enough).
teststring.length = delimiter.length * 2;
char c;Next we set up our variables,
int endpos = delimiter.length;
bool foundBoundary = false;
int lookup_offset = 0;
int teststring_offset = 0;
while (true) {Now we start reading the incomming stream, checking to see if we have reached the end of the stream.
if (!mimeSource.getChar(c)) {
eof = true;
break;
}
if (c == '\n') {We keep an eye on how many lines we have read.
nlines++;
}
if ((teststring_offset == 0) && (delimiter[0] != c)) {If we are looking for the first character, and it doesnt match, just keep reading!
writefln("first character does not match: %s != %s",
delimiter[0] , c );
continue;
}
teststring[teststring_offset] = c;We now add the character to our test string. (even if it doesnt match)
teststring_offset++;
if (delimiter[lookup_offset] == c) {Now we test to see if the character we got matches the expected one, and if we have reached the end of the delimiter, then stop processing. otherwise make sure the lookup offset is increased.
writefln("got a matching character match (%d/%d): %s == %s",
lookup_offset , endpos, delimiter[0] , c );
if ((lookup_offset + 1) == endpos) {
writefln("GOT FULL MATCHING STRING ");
foundBoundary = true;
break;
}
lookup_offset++;
continue; // go and find next character..
}
int trim_offset = 1;We start going through the test_string, starting at the second character, first off, we check to see if we have check all of the test_string, and just tell it to clean up if we have. (eg. nothing in this bit matches.)
while(true) {
writefln("testing teststring_offset=%d teststring[%d]
(%s) against first character %s",
teststring_offset, trim_offset,
teststring[trim_offset] , delimiter[0]);
if (trim_offset >= teststring_offset) { // reached the end..
writefln("Gone to end of string");
teststring_offset = 0;
lookup_offset = 0;
break;
}
if (teststring[trim_offset] == delimiter[0]) {Now we compare the section of the string against the portion of the delimiter, if they match, we rearrange the test_string by copying the string to the beginning, and reseting our pointers.
// found the start...
//check if string matches now..
int test_len = teststring_offset - trim_offset;
writefln("MATCH testing available remaining string
[%d..%d]%s == [%d]%s",
trim_offset,
test_len,
teststring[trim_offset..test_len] ,
test_len, delimiter[0..test_len]
);
if (teststring[trim_offset..test_len] == delimiter[0..test_len]) {
teststring[0..test_len] = teststring[trim_offset..test_len];
teststring_offset = 0;
lookup_offset = test_len;
break;
}
}
trim_offset++;
} }
return foundBoundary;To make this little test work, we need to create a simple stream reader.
}
class MimeStreamThen create a main() functions so we can test it.
{
char[] thestring = "";
int pos = 0;
this(char[] string) {
this.thestring = string;
}
bool getChar(inout char c)
{
if (pos >= thestring.length) {
return false;
}
c = this.thestring[pos];
pos++;
return true;
}
}
import std.stdio;and with a simple line, build a binary to test:
void main () {
MimeStream x = new MimeStream("This is a test - hello with XXX - hello world - in the middle".dup);
uint lines = 0;
bool eof = 0;
bool ret = skipUntilBoundary(x, "- hello world -".dup, lines, eof);
if (ret) {
writefln("GOT STRING!");
} else {
writefln("NO MATCH");
}
}
#/dmd/bin/dmd test_string.dAnd out comes our result: GOT STRING! (with a few more debugging messages preceeding it.)
#./test_string